import { ExceptionHandler } from './../../../../../handler/exception.handler';
import { Component, OnInit, OnDestroy } from '@angular/core';
import { NzMessageService } from 'ng-zorro-antd';
import { Subscription } from 'rxjs';
import { WorkflowPageService } from 'src/app/common/page-render/workflow-page-container/workflow-page.service';
import { BaseService, WorkflowService, DataRegisterService } from 'src/app/services';
import { EventManagerService } from 'src/app/services/event-manager.service';
import { MessageService } from 'src/app/services/message.service';
import { OrganizationService } from 'src/app/services/organization.service';
import { UploadService } from 'src/app/services/upload.service';
import { EngineService } from '../../../services/engine.service';
import { BaseFieldComponent } from '../base-field/base-field.component';

@Component({
  selector: 'zepride-sys-sync',
  templateUrl: './sys-sync.component.html',
  styleUrls: ['./sys-sync.component.less', '../field.common.less']
})
export class SysSyncComponent extends BaseFieldComponent implements OnInit, OnDestroy {
  workflowUpdatedSub: Subscription;

  constructor(
    protected engineService: EngineService,
    protected baseSvr: BaseService,
    protected eventSvr: EventManagerService,
    protected orgSvr: OrganizationService,
    public uploadService: UploadService,
    protected msg: NzMessageService,
    protected messageSvr: MessageService,
    protected workflowSvr: WorkflowService,
    private wSvr: WorkflowPageService,
    protected dataRegisterService: DataRegisterService
  ) {
    super(engineService, baseSvr, eventSvr, orgSvr, uploadService, msg, dataRegisterService);
  }

  ngOnDestroy(): void {
    if (this.workflowUpdatedSub) {
      this.workflowUpdatedSub.unsubscribe();
    }
  }

  ngOnInit(): void {
    this.engineService.componentHide$.next({
      tmpComItemId: this.tmpComItemId
    });

    this.workflowUpdatedSub = this.wSvr.workflowUpdated.subscribe(async data => {
      if (!data) {
        return;
      }
      this.dataRegister = data;
    });
  }

  pageSavedHandle = async () => {
    console.log('SysSyncComponent', this.mapDataModel);
    try {
      this.mapDataModel.ProjectInfo.YouJianProjectId = this.dataRegister.id;
      this.mapDataModel.ProjectInfo.OrganizationId = localStorage.getItem('orgid');
      await this.dataRegisterService.syncDataToSAupplierSys(this.mapDataModel);
    } catch (error) {
      ExceptionHandler.current.handleError(error);
    }
  }

}
